package table;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class SetDataParamenters {
	private int startPage ;
	private int pageSize ;
	private List<String[]> queryList ;
	private List<String[]> sortList ;
	private List<String> dstColNameList ;
	private boolean isDistinct;
	
	/**
	 * Get List method paramenters from request 
	 * @param request
	 * @return Map Object which map List Method Paraments 
	*/
	public SetDataParamenters(){
		this.setStartPage(0);
		this.setPageSize(0);
		this.setQueryList(null);
		this.setSortList(null);
	} 
	public SetDataParamenters(HttpServletRequest request){
		this.setStartPage(getStartPageFromRequest(request));
		this.setPageSize(getPageSizeFromRequest(request));
		this.setQueryList(getQueryListFromRequest(request));
		this.setSortList(getSortListFromRequest(request));
	}
	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}
	public int getStartPage() {
		return startPage;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setQueryList(List<String[]> queryList) {
		this.queryList = queryList;
	}
	public List<String[]> getQueryList() {
		return queryList;
	}
	public void setSortList(List<String[]> sortList) {
		this.sortList = sortList;
	}
	public List<String[]> getSortList() {
		return sortList;
	}
	
	

	public void clearQuery(){
		if(this.queryList != null){
			this.queryList.clear(); 
		}
	}
	public void removeQuery(String name){
		if(this.queryList != null){
			for(int i = 0;  i < this.queryList.size(); i++){
				String[] r = this.queryList.get(i);
				if(r[0].equals(name)){
					this.queryList.remove(i);
				}
			} 
		}
	}
	public void setQuery(String name, String value){
		if(this.queryList == null){
			this.queryList = new ArrayList<String[]>(); 
		}
		String[] r = null;
		try {
			r = new String[3];
			r[0] = name; 
			r[1] = value;
			r[2] = "";
			this.queryList.add(r);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Get Start Page from request
	 * @param request
	 * @return Start Page Parament as integer
	 */
	static public int getStartPageFromRequest(HttpServletRequest request){
		int s = 0;
		if(null != request.getParameter("start")){
			String sPageStart = request.getParameter("start");
			if(sPageStart != null && sPageStart.toLowerCase().matches("([1-9][0-9]*)|([0-9])"))
			s = Integer.parseInt(sPageStart);
		}
		return s;
	}
	
	/**
	 * Get Page Size from request
	 * @param request
	 * @return Page Size Parament as integer
	 */
	static public  int getPageSizeFromRequest(HttpServletRequest request){
		int n = 200;
		if(null != request.getParameter("pagesize")){
			String sPageSize = request.getParameter("pagesize");
			if(sPageSize != null && sPageSize.toLowerCase().matches("([1-9][0-9]*)"))
			n = Integer.parseInt(sPageSize);
		}
		return n;
	}

	/**
	 * get query paramenters from request paramenters 
	 * @param request
	 * @return Query Parament Names and Values as List of Strings
	 */
	static public  List<String[]> getQueryListFromRequest(HttpServletRequest request){
		List<String[]> s = new ArrayList<String[]>();
		String[] r = null;
		String colName = "";
		String colValue = "";
		try {
			Enumeration<String> paraNames = request.getParameterNames();
			while(paraNames.hasMoreElements()){
				r = new String[3];
				colName = paraNames.nextElement();
				if(colName.startsWith("q-")){
					colValue = request.getParameter(colName);
					if(null != colValue && colValue.length()>0){
						r[0] = colName.substring(2); 
						r[1] = colValue;
						s.add(r);
					}
				}
			}
		} catch (Exception e) {
				e.printStackTrace();
		}
		return s;
	}
	/**
	 * get sort paramenters from request paramenters 
	 * @param request
	 * @return Sort Parament Names and Values as List of Strings
	 */
	static public  List<String[]> getSortListFromRequest(HttpServletRequest request){
		List<String[]> s = new ArrayList<String[]>();
		String colName = "";
		String colValue = "";
		String[] r = new String[2];
		
		Enumeration<String> paraNames = request.getParameterNames();
		while(paraNames.hasMoreElements()){
			colName = paraNames.nextElement();
			if(colName.startsWith("s-")){
				colValue = request.getParameter(colName);
				if(null != colValue && colValue.length()>0){
					r[0] = colName.substring(2); 
					if(colValue.equalsIgnoreCase("DESC")){
						r[1]= "DESC";
					}else{
						r[1]= "ASC";
					}
					s.add(r);
				}
			}
		}
		return s;
	}
	public void setDstColNameList(List<String> dstColNameList) {
		this.dstColNameList = dstColNameList;
	}
	public List<String> getDstColNameList() {
		return dstColNameList;
	}
	public void addDstColName(String colName){
		if(this.dstColNameList == null){
			this.dstColNameList = new ArrayList<String>(); 
		}
		try {
			this.getDstColNameList().add(colName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void setDistinct(boolean isDistinct) {
		this.isDistinct = isDistinct;
	}
	public boolean isDistinct() {
		return isDistinct;
	}
}
